package com.hahehiho.myalbum;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class BackupInfoStatus extends Thread{
	
	private String path;
	private int percentage;
	private String startTime = "";
	private String endTime = "";
	private boolean closed = true;
	private boolean isAll;
	
	synchronized public void runRecreate(String path, boolean isAll) {
		closed = false;
		this.path = path;
		this.isAll = isAll;
		
		setPercentage(0);
		setStartTime("");
		setEndTime("");
		setDaemon(true);
		
		start();
	}
	
	public void run() {
		setStartTime();		
		setPercentage(1);
		try {

			File file = new File(path);
			FileListIndexer indexer = new FileListIndexer(file);
			indexer.createIndex();
			
			sendToKTCloud(indexer);

		} catch(Throwable t) {
			t.printStackTrace();
		}
		closed = true;
		setEndTime();
	}
	
	private void sendToKTCloud(FileListIndexer indexer) {
		int size = indexer.getItems().size();
		
		Map<String, String> items = indexer.getItems();
		
		Iterator<String> it = items.values().iterator();
		int count = 0;
		while(it.hasNext()) {
			String filePath = it.next();
			
			
			
			count++;
			setPercentage(count*100/size);
		}
	}

	private void setEndTime() {
		setEndTime(getCurrentTime());
	}

	private void setStartTime() {
		setStartTime(getCurrentTime());
	}

	private String getCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

		return sdf.format(new Date());
	}

	public boolean isClose() {
		return closed ;
	}
	
	public int currentPercentage() {
		return percentage;
	}
	
	public String startTime() {
		return startTime;
	}
	
	public String endTime() {
		return endTime;
	}

	public int getPercentage() {
		return percentage;
	}

	public void setPercentage(int percentage) {
		this.percentage = percentage;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
